package core;

// Restraints - if it is an item barcode it must be unique and UPC-A, 
//              if it is a product or an item it can't be empty

/**
 * Barcode Class is a wrapper class around a string barcode
 */
public class Barcode extends ModelObject 
{	
	/**
	 * 
	 */
	private static final long serialVersionUID = -5026980276007692983L;
	
	/** barcode is the string representation of the barcode
	 */
	protected String barcode;
	
	/** Returns a Barcode object
	 * @param bCode the string to be a barcode
	 */
	public Barcode(String bCode) throws IllegalArgumentException
	{
		assert(isValidBarcode(bCode));
		if(!isValidBarcode(bCode))
			throw new IllegalArgumentException();
		barcode = bCode;
	}
	
	/** Returns the string barcode
	 * 
	 * @return string value of the Barcode object
	 */
	public String getBarcode()
	{
		return barcode;
	}
	
    /** setter for barcode
     * 
     * @param newBarCode the new value of the barcode
     * @return true if set, false if did not set
     */
    public boolean setBarCode(String newBarCode)
    {
    	assert(!newBarCode.isEmpty());
    	if(!isValidBarcode(newBarCode))
    	{
    		throw new IllegalArgumentException();
    	}
        barcode = newBarCode;
        return true;
    }
        
    /**
     * Checks for a valid barcode when not given a specific string, 
     * it will us this.barcode
     * @return true if barcode is valid, false otherwise
     */
    public boolean isValidBarcode()
    {
    	String bCode = this.barcode;
    	return isValidBarcode(bCode);
    }
    
    /**
     * Checks to see if the string passed in is a valid barcode
     * @param bCode the barcode to check
     * @return true if bCode is valid, false otherwise
     */
    public boolean isValidBarcode(String bCode)
    {
    	assert(!bCode.isEmpty());
    	
    	if(bCode.isEmpty())
    		return false;
    	
    	return true;
    }

    /**
     * Override of the equals operator
     * @param obj the object to compare to this
     * @return true if they are equal, false otherwise
     */
    @Override
    public boolean equals(Object obj)
    {
    	if (obj == null) return false;
    	if (obj == this) return true;
    	if (obj.getClass() != this.getClass()) return false;
    	
    	Barcode barcode = (Barcode) obj;
    	
    	return (barcode.barcode.compareTo(this.barcode) == 0);
    }
    
    /**
     * hash code function
     * @return hash value
     */
    @Override
    public int hashCode()
    {
    	return barcode.hashCode();
    }
    
}
